package cn.itsy.web.controller;


import cn.itsy.dao.LituixiuDao;
import cn.itsy.dao.Pagedao;
import cn.itsy.pojo.*;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;


import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@Controller
@RequestMapping("lituixiu")
public class LituixiuController {
    @Resource
    private LituixiuDao lz;
    @Resource
    private Pagedao pd;

    @RequestMapping("list")
    public String show(HttpServletResponse response, HttpServletRequest request, String empname, String dname, String sex, String education, String paixu, String currentPage) {

        String sql = "select e.id,e.empid,e.empname,d.dname,e.sex,e.idcard,e.birthday,e.natives,e.nation,e.education,e.is_status " +
                " from emp e,empstatus es, dept d where d.id=e.depart_id and e.is_status=es.id and es.id= 8 and  d.id=e.depart_id  ";
        Page page = new Page();
        System.out.println(empname);
        if (empname != null && !empname.equals("")) {
            sql += " and empname like '%" + empname + "%'";
        }

        if (dname != null && !dname.equals("")) {
            sql += " and dname like '%" + dname + "%'";
        }

        if (sex != null && !sex.equals("")) {
            sql += " and sex like '%" + sex + "%'";
        }

        if (education != null && !education.equals("")) {
            sql += " and education like '%" + education + "%'";
        }

        if (paixu != null && !paixu.equals("1")) {
            sql += " ORDER BY id asc ";
        }
        if (paixu != null && !paixu.equals("2")) {
            sql += " ORDER BY id desc ";
        }
        Page empfy = pd.empfy(sql, currentPage, page);
        String sql1 = "select * from dept";
        List<Dept> ml = lz.queryDept(sql1);
        request.setAttribute("ml", ml);
        System.out.println(sql);
        request.setAttribute("empfy", empfy);
        return "/lituixiu";
    }

    @RequestMapping("list1")
    public String emplist(HttpServletRequest request, HttpServletResponse response, String empname, String dname, String sex, String education, String isStatus, String jobId, String ductId, String currentPage) {
        String sql = "select e.id,e.empid,e.empname,e.officephone,e.poleticalstatus,d.dname,du.zname,e.sex,e.idcard,e.birthday,e.natives,j.name,e.nation,e.education,e.is_status " +
                " from emp e,duct du,empstatus es,job j, dept d where d.id=e.depart_id and du.id=e.duct_id and j.id=e.job_id and e.is_status=es.id and d.id=e.depart_id  ";
        Page page = new Page();
        if (empname != null && !empname.equals("")) {
            sql += " and empname like '%" + empname + "%'";
        }

        if (dname != null && !dname.equals("")) {
            sql += " and dname like '%" + dname + "%'";
        }

        if (sex != null && !sex.equals("")) {
            sql += " and sex like '%" + sex + "%'";
        }

        if (isStatus != null && !isStatus.equals("")) {
            sql += " and is_status = '" + isStatus + "'";
        }
        if (education != null && !education.equals("")) {
            sql += " and education like '%" + education + "%'";
        }
        if (jobId != null && !jobId.equals("")) {
            sql += " and job_id ='" + jobId + "'";
        }
        if (ductId != null && !ductId.equals("")) {
            sql += " and e.duct_id ='" + ductId + "'";
        }
        String sql1 = "select * from dept";
        List<Dept> ml = lz.queryDept(sql1);
        request.setAttribute("ml", ml);

        String sql2 = "select * from job group by name";
        List<Job> jobs = lz.JobAll(sql2);
        request.setAttribute("jobAll", jobs);

        String sql3 = "select * from duct";
        List<Duct> ducts = lz.ducts(sql3);
        request.setAttribute("ducts", ducts);

        Page empfy2 = pd.empfy(sql, currentPage, page);
        request.setAttribute("empfy2", empfy2);
        return "/empList";
    }


    @RequestMapping("del")
    public String delEmp(HttpServletRequest request, HttpServletResponse response, String empid) {
        String sql = "delete from emp where empid = '" + empid + "'";
        lz.updateEmp(sql);


        return "redirect:list";
    }

    @RequestMapping("/addemp")
    public String addEmp(HttpServletRequest request, HttpServletResponse response) {
        String sql1 = "select * from dept";
        String sql = "select * from job group by name";
        List<Dept> depts = lz.queryDept(sql1);
        List<Job> jobs = lz.JobAll(sql);
        request.setAttribute("depts", depts);
        request.setAttribute("jobAll", jobs);
        return "/addCitui";
    }

    @RequestMapping("/add")
    public String insertAdd(HttpServletRequest request, HttpServletResponse response, Emp e) {
        System.out.println(e.toString());
        String sqls = "select * from emp where empid ='" + e.getEmpid() + "'";
        List<Emp> empList = lz.emplist(sqls);
        if (empList.isEmpty()) {
            System.out.println("可以添加");
            String sql = "insert into emp values (default,'" + e.getEmpid() + "','" + e.getEmpname() + "'," +
                    "'" + e.getDepartId() + "','0','" + e.getEnglishname() + "','" + e.getNameusedbefore() + "'," +
                    "'" + e.getSex() + "','" + e.getIdcard() + "','" + e.getBirthday() + "','" + e.getNatives() + "','" + e.getNation() + "'," +
                    "'" + e.getPoleticalstatus() + "','" + e.getReligiousbelief() + "','" + e.getHealthcondition() + "',''," +
                    "'" + e.getMaritalstatus() + "','" + e.getWagesaccount() + "','" + e.getSocialsecuritynumbers() + "'," +
                    "'" + e.getPartworktime() + "','" + e.getEntertime() + "','" + e.getPlace() + "','" + e.getEducation() + "'," +
                    "'" + e.getJobId() + "','" + e.getOfficephone() + "','" + e.getHomephone() + "','" + e.getMobilephone() + "'," +
                    "'" + e.getIsStatus() + "')";
            lz.updateEmp(sql);
            request.setAttribute("check", "success");
            return "/list";
        }
        System.out.println("重复添加");
        request.setAttribute("check", "error");
        return "/addCitui";

    }

    @RequestMapping("toupdate")
    public String toupdate(HttpServletRequest request, HttpServletResponse response, String id) {
        String sql = "select * from emp where id='" + id + "'";
        List<Emp> emps = lz.emplist(sql);
        request.setAttribute("emps", emps);

        String sql1 = "select * from dept";
        List<Dept> depts = lz.queryDept(sql1);
        request.setAttribute("depts", depts);
        return "/empupdate";
    }

    @RequestMapping("update")
    public String update(Emp e) {
        String sql = "update emp set empid='" + e.getEmpid() + "',empname='" + e.getEmpname() + "',depart_id='" + e.getDepartId() + "',sex='" + e.getSex() + "',idcard='" + e.getIdcard() + "',birthday='" + e.getBirthday() + "',education='" + e.getEducation() + "',natives='" + e.getNatives() + "',nation='" + e.getNation() + "' where id='" + e.getId() + "'";
        lz.updateEmp(sql);
        return "redirect:list";
    }

    @RequestMapping("chakan")
    public String chakan(HttpServletRequest request, HttpServletResponse response, String id) {
        String sql = "select * from emp where id='" + id + "'";
        List<Emp> emps = lz.emplist(sql);
        request.setAttribute("emps", emps);

        String sql1 = "select * from dept";
        List<Dept> depts = lz.queryDept(sql1);
        request.setAttribute("depts", depts);
        return "/chakanemp";
    }

    //查询不在职记录
    @RequestMapping("EmpJiLu")
    public String EmpJiLu(HttpServletRequest request, HttpServletResponse response, String id) {
        String sql = " select e.id,e.empid,e.empname,d.dname,t.zname,e.sex,e.idcard,e.birthday,e.education,j.name,e.officephone,y.changetime,\n" +
                "y.changdesc,y.type,s.status from emp e,empstatus s,dept d,job j,duct t,emphistory y where e.is_status = s.id and e.depart_id = d.id and e.job_id = j.id and e.duct_id = t.id and y.emp_id = e.id and e.is_status = '8' and e.id='" + id + "'";
        List<Emp> eps = lz.emplist(sql);
        request.setAttribute("eps", eps);
        return "/lituiJiLu";
    }
}
